from functools import lru_cache
from os import path

from pydantic import BaseModel

ROOT_PATH = path.dirname(path.abspath(path.join(__file__, '..')))

class Settings(BaseModel):

    # 数据源配置
    database_url: str = 'mysql+pymysql://root:123123@127.0.0.1:3306/web2024?charset=utf8mb4'
    # 数据库连接池最小值
    database_pool_min_size: int = 5
    # 数据库连接池最大值
    database_pool_max_size: int = 20
    # 数据库连接最大空闲时间
    database_pool_recycle: int = 300

    # Redis源配置
    redis_host: str = '127.0.0.1'
    redis_port: int = 6379
    redis_pwd: str = '123123'
    redis_db: int = 1

    # CORS 跨域资源共享
    # 允许跨域的源列表 eg. '["*"]'   '["http://localhost", "http://localhost:8080", "https://www.example.org"]'
    cors_allow_origins: str = '["*"]'

    # 全局配置
    # 版本
    version: str = 'v1.1.0'
    # 项目根路径
    root_path: str = ROOT_PATH
    # 默认请求超时
    request_timeout: int = 15
    # 绑定IP
    bind_ip: str = "0.0.0.0"
    bind_port: int = 9908
    autoload: bool = True


@lru_cache()
def get_settings() -> Settings:
    """获取并缓存应用配置"""
    return Settings()
